package cn.net.yugu.doraemon.common.arms;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

import com.alibaba.arms.sdk.v1.async.wrapper.CommandWrapper;
import org.springframework.core.task.TaskDecorator;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

/**
 * @author xiangzhurui
 * @version 2023/8/11 22:21:08
 */
public class TraceThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {

  protected final ThreadPoolTaskExecutor delegate;
  protected final CommandWrapper wrapper;

  public TraceThreadPoolTaskExecutor(ThreadPoolTaskExecutor taskExecutor, CommandWrapper wrapper) {
    this.delegate = taskExecutor;
    this.wrapper = wrapper;
  }

  @Override
  public void setCorePoolSize(int corePoolSize) {
    delegate.setCorePoolSize(corePoolSize);
  }

  @Override
  public int getCorePoolSize() {
    return delegate.getCorePoolSize();
  }

  @Override
  public void setMaxPoolSize(int maxPoolSize) {
    delegate.setMaxPoolSize(maxPoolSize);
  }

  @Override
  public int getMaxPoolSize() {
    return delegate.getMaxPoolSize();
  }

  @Override
  public void setKeepAliveSeconds(int keepAliveSeconds) {
    delegate.setKeepAliveSeconds(keepAliveSeconds);
  }

  @Override
  public int getKeepAliveSeconds() {
    return delegate.getKeepAliveSeconds();
  }

  @Override
  public void setQueueCapacity(int queueCapacity) {
    delegate.setQueueCapacity(queueCapacity);
  }

  @Override
  public int getQueueCapacity() {
    return delegate.getQueueCapacity();
  }

  @Override
  public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
    delegate.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);
  }

  @Override
  public void setPrestartAllCoreThreads(boolean prestartAllCoreThreads) {
    delegate.setPrestartAllCoreThreads(prestartAllCoreThreads);
  }

  @Override
  public void setTaskDecorator(TaskDecorator taskDecorator) {
    delegate.setTaskDecorator(taskDecorator);
  }

  @Override
  public ThreadPoolExecutor getThreadPoolExecutor() throws IllegalStateException {
    return delegate.getThreadPoolExecutor();
  }

  @Override
  public int getPoolSize() {
    return delegate.getPoolSize();
  }

  @Override
  public int getQueueSize() {
    return delegate.getQueueSize();
  }

  @Override
  public int getActiveCount() {
    return delegate.getActiveCount();
  }

  @Override
  public void execute(Runnable task) {
    delegate.execute(wrapper.wrap(task));
  }

  @Override
  @Deprecated
  public void execute(Runnable task, long startTimeout) {
    delegate.execute(wrapper.wrap(task), startTimeout);
  }

  @Override
  public Future<?> submit(Runnable task) {
    return delegate.submit(wrapper.wrap(task));
  }

  @Override
  public <T> Future<T> submit(Callable<T> task) {
    return delegate.submit(wrapper.wrap(task));
  }

  @Override
  public ListenableFuture<?> submitListenable(Runnable task) {
    return delegate.submitListenable(wrapper.wrap(task));
  }

  @Override
  public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
    return delegate.submitListenable(wrapper.wrap(task));
  }

  @Override
  public void setThreadFactory(ThreadFactory threadFactory) {
    delegate.setThreadFactory(threadFactory);
  }

  @Override
  public void setThreadNamePrefix(String threadNamePrefix) {
    delegate.setThreadNamePrefix(threadNamePrefix);
  }

  @Override
  public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) {
    delegate.setRejectedExecutionHandler(rejectedExecutionHandler);
  }

  @Override
  public void setWaitForTasksToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown) {
    delegate.setWaitForTasksToCompleteOnShutdown(waitForJobsToCompleteOnShutdown);
  }

  @Override
  public void setAwaitTerminationSeconds(int awaitTerminationSeconds) {
    delegate.setAwaitTerminationSeconds(awaitTerminationSeconds);
  }

  @Override
  public void setAwaitTerminationMillis(long awaitTerminationMillis) {
    delegate.setAwaitTerminationMillis(awaitTerminationMillis);
  }

  @Override
  public void setBeanName(String name) {
    delegate.setBeanName(name);
  }

  @Override
  public void afterPropertiesSet() {
    delegate.afterPropertiesSet();
  }

  @Override
  public void initialize() {
    delegate.initialize();
  }

  @Override
  public void destroy() {
    delegate.destroy();
  }

  @Override
  public void shutdown() {
    delegate.shutdown();
  }

  @Override
  public Thread newThread(Runnable runnable) {
    return delegate.newThread(runnable);
  }

  @Override
  public String getThreadNamePrefix() {
    return delegate.getThreadNamePrefix();
  }

  @Override
  public void setThreadPriority(int threadPriority) {
    delegate.setThreadPriority(threadPriority);
  }

  @Override
  public int getThreadPriority() {
    return delegate.getThreadPriority();
  }

  @Override
  public void setDaemon(boolean daemon) {
    delegate.setDaemon(daemon);
  }

  @Override
  public boolean isDaemon() {
    return delegate.isDaemon();
  }

  @Override
  public void setThreadGroupName(String name) {
    delegate.setThreadGroupName(name);
  }

  @Override
  public void setThreadGroup(ThreadGroup threadGroup) {
    delegate.setThreadGroup(threadGroup);
  }

  @Override
  @Nullable
  public ThreadGroup getThreadGroup() {
    return delegate.getThreadGroup();
  }

  @Override
  public Thread createThread(Runnable runnable) {
    return delegate.createThread(runnable);
  }

  @Override
  public boolean prefersShortLivedTasks() {
    return delegate.prefersShortLivedTasks();
  }
}
